package com.zhz.lib.cms.manage.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.zhz.lib.cms.domain.quanxian.GroupMembers;
import com.zhz.lib.cms.domain.quanxian.Groups;
import com.zhz.lib.cms.domain.quanxian.User;
import com.zhz.lib.cms.utils.zhz.ZhzSecurityUtil;
import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.common.util.hibernate.BaseDAO;
import com.zhz.project.common.util.jiami.MD5Builder;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;

public class userAction extends DispatchAction {

    private BaseDAO baseDao;

    /**
     * 不给参数，默认执行此处
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
     *      org.apache.struts.action.ActionForm,
     *      javax.servlet.http.HttpServletRequest,
     *      javax.servlet.http.HttpServletResponse)
     */
    @Override
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response)
                                                                                              throws Exception {
        return userview(mapping, form, request, response);
    }

    /**
     * 查看用户信息
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward userview(ActionMapping mapping, ActionForm form,
                                  HttpServletRequest request, HttpServletResponse response) {

        User user = (User) baseDao.findOneObject("User", "id", ZhzSecurityUtil.getOperator()
            .getId());

        GroupMembers gm = (GroupMembers) baseDao.findOneObject("GroupMembers", "username", user
            .getUsername());
        Groups g = null;
        if (null != gm)
            g = (Groups) baseDao.findOneObject("Groups", "id", gm.getGroupId().toString());

        request.setAttribute("user", user);
        request.setAttribute("groups", g);

        return mapping.findForward("user-view");
    }

    /**
     * 编辑用户信息
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward useredit(ActionMapping mapping, ActionForm form,
                                  HttpServletRequest request, HttpServletResponse response) {

        User user = (User) baseDao.findOneObject("User", "id", ZhzSecurityUtil.getOperator()
            .getId());

        request.setAttribute("user", user);

        return mapping.findForward("user-edit");
    }

    /**
     * 保存编辑后的用户
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward usereditsave(ActionMapping mapping, ActionForm form,
                                      final HttpServletRequest request, HttpServletResponse response) {

        String id = ZhzHttpRequestUtil.getParameters(request, "id");

        String name = ZhzHttpRequestUtil.getParameters(request, "name");

        String email = ZhzHttpRequestUtil.getParameters(request, "email");

        User user = (User) baseDao.findOneObject("User", "id", id);
        user.setName(name);
        user.setEmail(email);

        boolean edituserresult = baseDao.update(user);

        if (edituserresult) {
            request.setAttribute("result", "编辑成功");
        } else {
            request.setAttribute("result", "编辑失败");
        }
        /**
         * 返回
         */

        return mapping.findForward("user-result");
    }

    /**
     * 编辑登录密码
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward usereditpwd(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response) {

        User user = (User) baseDao.findOneObject("User", "id", ZhzSecurityUtil.getOperator()
            .getId());

        request.setAttribute("user", user);

        return mapping.findForward("user-editpwd");
    }

    /**
     * 保存编辑后的密码
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward usereditpwdsave(ActionMapping mapping, ActionForm form,
                                         final HttpServletRequest request,
                                         HttpServletResponse response) {

        String id = ZhzHttpRequestUtil.getParameters(request, "id");

        String password1 = ZhzHttpRequestUtil.getParameters(request, "password1");
        String password2 = ZhzHttpRequestUtil.getParameters(request, "password2");

        User user = (User) baseDao.findOneObject("User", "id", id);

        if (!StringUtil.equals(user.getPassword(), MD5Builder.getMD5(password1))) {
            request.setAttribute("result", "旧密码输入错误！");
            return mapping.findForward("user-result");
        }

        user.setPassword(MD5Builder.getMD5(password2));

        boolean edituserresult = baseDao.update(user);

        if (edituserresult) {
            request.setAttribute("result", "修改登录密码成功");
        } else {
            request.setAttribute("result", "修改登录密码失败");
        }
        return mapping.findForward("user-result");
    }

    public void setBaseDao(BaseDAO baseDao) {
        this.baseDao = baseDao;
    }
}